/**
 * 
 */
package br.com.cnpv.modelo.bo;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;

import br.com.cnpv.modelo.acesso.idao.IAssociacaoDAO;
import br.com.cnpv.modelo.dao.AssociacaoDAO;
import br.com.cnpv.modelo.entity.Associacao;
import br.com.cnpv.util.HibernateUtil;

/**
 * @author ricardo
 *
 */
public class AssociacaoBO extends HibernateUtil {

	public static AssociacaoBO INSTANCE;
	
	public static AssociacaoBO getInstance()
	{
		if(INSTANCE == null)
		{
			synchronized (AssociacaoBO.class) {
				INSTANCE = new AssociacaoBO();
			}
		}
		return INSTANCE;
	}

	public List<Associacao> buscar(Associacao associacao) throws Exception
	{
		Session session = null;
		List<Associacao> list = null;
		try
		{
			session = currentSession();
			IAssociacaoDAO dao = new AssociacaoDAO();
			list = dao.buscar(associacao, session);
		}catch(Exception e)
		{
			throw new Exception(e);
		}finally
		{
			if(session != null)
			{
				session.flush();
				session.clear();
				closeSession();
			}
		}
		return list;
	}

	public void saveOrUpdate(Associacao associacao) throws Exception{
		Session session = null;
		try
		{
			session = currentSession();
			IAssociacaoDAO dao = new AssociacaoDAO();
			associacao.setDtIncl(new Date());
			dao.saveOrUpdate(associacao, session);
		}catch(Exception e)
		{
			throw new Exception(e);
		}finally
		{
			if(session != null)
			{
				session.flush();
				session.clear();
				closeSession();
			}
		}
		
	}

	public List<Associacao> listar() throws Exception
	{
		Session session = null;
		List<Associacao> list = null;
		try
		{
			session = currentSession();
			IAssociacaoDAO dao = new AssociacaoDAO();
			list = dao.listar(session);
		}catch(Exception e)
		{
			throw new Exception(e);
		}finally
		{
			if(session != null)
			{
				session.flush();
				session.clear();
				closeSession();
			}
		}
		return list;
	}

}
